package com.vol.controller.sys;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.vol.entity.Result;
import com.vol.entity.SysMenu;
import com.vol.utils.UserContext;
import com.vol.entity.UserInfo;
import com.vol.sys.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("api/menu")
public class MenuController {

    @Autowired
    private UserContext userContext;
    @Autowired
    private MenuService menuService;

    @GetMapping("getTreeMenu")
    public List<Map<String, Object>> getTreeMenu() throws JsonProcessingException {
        //获取菜单信息
        List<Map<String, Object>> listmap = menuService.getMenuMap();
        return listmap;
    }

    @PostMapping("getMenu")
    public List<Map<String, Object>> getMenu() {
        List<Map<String, Object>> list = menuService.getMeunConsoleMap();
        return list;
    }

    @PostMapping("getTreeItem")
    public ResponseEntity<Map<String, Object>> getTreeItem(int menuId) {
        Map<String, Object> map = menuService.getTreeItem(menuId);
        return ResponseEntity.ok(map);
    }

    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu) throws JsonProcessingException {
        if (sysMenu.getMenuname() == null) {
            return Result.build(null, -1, "菜单名称不能为空!", false);
        }
        menuService.save(sysMenu);
        return Result.ok(sysMenu, null, "数据更新成功！");
    }

    @PostMapping("delMenu")
    public Result delMenu(Integer menuId) throws JsonProcessingException {
        menuService.del(menuId);
        return Result.ok(null, null, "删除成功！");
    }

}

